package org.seafloor.system.dao.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.seafloor.common.dao.Impl.BaseDaoImpl;
import org.seafloor.system.dao.UserDao;
import org.seafloor.system.model.UserVO;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

public class UserDaoImpl extends BaseDaoImpl<UserVO> implements UserDao {

	public List<UserVO> findUserList() throws Exception {
		return (List<UserVO>) hibernateTemplate.find("from UserVO");
	}

	public UserVO findUserById(UserVO userVO) throws Exception {
		return hibernateTemplate.get(userVO.getClass(), userVO.getUserId());
	}
	
	public UserVO findUserByLoginName(UserVO userVO) throws Exception {
		List<UserVO> userList=hibernateTemplate.find("from UserVO u where u.loginName=?", userVO.getLoginName());
		if(null==userList||userList.isEmpty()){
			return null;
		}
		return userList.get(0);
	}

	public String saveUser(UserVO userVO) throws Exception {
		userVO.setCreateTime(new Date());
		hibernateTemplate.save(userVO);
		return "";
	}

	public void updateUser(UserVO userVO) throws Exception {
		hibernateTemplate.update(userVO);
	}

	public void updateUserLastLogin(UserVO userVO) throws Exception {
		UserVO u=hibernateTemplate.get(userVO.getClass(), userVO.getUserId());
		u.setLastLogin(new Date());
		hibernateTemplate.update(u);
	}

	public int deleteUser(UserVO userVO) throws Exception {
		hibernateTemplate.delete(userVO);
		return 0;
	}

}
